package com.anjubao.parking.municipal.roadside.service;

import com.alibaba.fastjson.JSON;
import com.anjubao.parking.municipal.common.exception.ServiceException;
import com.anjubao.parking.municipal.roadside.dto.SlotStateResDto;
import com.anjubao.parking.municipal.roadside.form.ParkingLotSlotForm;
import com.anjubao.parking.municipal.roadside.mapper.ParkingLotMapper;
import com.anjubao.parking.municipal.roadside.mapper.ParkingLotSlotMapper;
import com.anjubao.parking.municipal.roadside.mapper.ParkingRecordMapper;
import com.anjubao.parking.municipal.roadside.model.ParkingLot;
import com.anjubao.parking.municipal.roadside.model.ParkingLotSlot;
import com.anjubao.parking.municipal.roadside.model.ParkingRecord;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ParkingLotSlotService {


    @Autowired
    private ParkingLotSlotMapper parkingLotSlotMapper;
    @Autowired
    private ParkingLotMapper parkingLotMapper;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;

    public void syncParkingLotSlot(int type, ParkingLotSlotForm form){
        QueryWrapper<ParkingLot> lotWrapper=new QueryWrapper<>();
        lotWrapper.eq("code", form.getParkingLotCode());
        ParkingLot parkingLot=parkingLotMapper.selectOne(lotWrapper);
        if (parkingLot==null) throw new ServiceException("无该车场，新增或者修改车位信息失败！");
        ParkingLotSlot parkingLotSlot = parkingLotSlotMapper.selectById(form.getId());
        if (type==1){//新增
            if (parkingLotSlot!=null) throw new ServiceException("车位已存在！");
            parkingLotSlot =new ParkingLotSlot();
            parkingLotSlot.setId(form.getId());
            parkingLotSlot.setCode(form.getCode());
            parkingLotSlot.setParkingLotId(parkingLot.getId());
            parkingLotSlot.setSlotNum(form.getSlotNum());
           /* parkingLotSlot.setIsChargingSlot(form.getIsChargingSlot());
            parkingLotSlot.setCharginghubNo(form.getCharginghubNo());
            parkingLotSlot.setCharginghubGunNo(form.getCharginghubGunNo());*/
            parkingLotSlotMapper.insert(parkingLotSlot);
        }
        if (type==2){//修改
            if (parkingLotSlot==null) throw new ServiceException("无该车位信息，不能修改！");
            parkingLotSlot.setSlotNum(form.getSlotNum());
            parkingLotSlot.setIsChargingSlot(form.getIsChargingSlot());
            parkingLotSlot.setCharginghubNo(form.getCharginghubNo());
            parkingLotSlot.setCharginghubGunNo(form.getCharginghubGunNo());
            parkingLotSlotMapper.updateById(parkingLotSlot);
        }
    }

    /**
     * 删除车位
     * @param parkingLotSlotCode
     */
    public void delParkingLotSlot(String  parkingLotSlotCode){
        QueryWrapper<ParkingLotSlot> slotDelWrapper=new QueryWrapper<>();
        slotDelWrapper.eq("code",parkingLotSlotCode);
        parkingLotSlotMapper.delete(slotDelWrapper);
    }

    /**
     * 获取车位状态
     * @param slotList
     * @return
     */
    public List<SlotStateResDto> getSlotState(List<String> slotList){
        if (slotList==null || slotList.size()==0){
            throw new ServiceException("车位编号不能为空");
        }
        List<SlotStateResDto> resDtos=new ArrayList<>();
        List<ParkingRecord> parkingRecords=parkingRecordMapper.getSlotState(slotList);
        //所有车位都没车
        if (parkingRecords==null || parkingRecords.size()==0){
            slotList.stream().forEach(i->{
                resDtos.add(new SlotStateResDto().setSlotNum(i).setState(0).setEnableMonitor(0).setParkingRecordId(""));
            });
            return resDtos;
        }
        //部分或者所有车位有车
        if (parkingRecords!=null && parkingRecords.size()>0){
            for (String slotNum:slotList) {
                SlotStateResDto resDto=new SlotStateResDto();
                resDto.setSlotNum(slotNum);
                List<ParkingRecord> records=parkingRecords.stream().filter(i->i.getSlotNum().equals(slotNum)).
                        collect(Collectors.toList());
                if (records==null || records.size()==0){//空闲
                    resDto.setState(0);
                    resDto.setEnableMonitor(0);
                    resDto.setParkingRecordId("");
                }
                if (records!=null && records.size()>0){
                    ParkingRecord parkingRecord=records.get(0);
                    resDto.setEnableMonitor(parkingRecord.getEnableMonitor());
                    resDto.setParkingRecordId(parkingRecord.getId());
                    if (parkingRecord.getFeeState()==0){//中杆停车未缴费
                        resDto.setState(1);
                        resDtos.add(resDto);
                        continue;
                    }
                    if (parkingRecord.getParkingExpireTime().after(new Date())){//停车未超时
                        resDto.setState(2);
                    }else {//停车超时
                        resDto.setState(3);
                    }
                }
                resDtos.add(resDto);
            }
        }
        return resDtos;
    }


}
